//
//  LXPermissionPush.swift
//  LXPermissionAuth
//
//  Created by yoctech on 2021/8/18.
//

import UIKit
import UserNotifications

@objc class LXPermissionPush: NSObject, LXPermissionProtocol {
    @objc public static func auth(autoRequestIfNot: Bool, clouser: @escaping LXAuthClouser) {
        UNUserNotificationCenter.current().getNotificationSettings(){ (setttings) in
            switch setttings.authorizationStatus {
            case .notDetermined:
                if !autoRequestIfNot {
                    clouser(false, .other)
                    return
                }
                UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .carPlay, .sound]) { (result, error) in
                    if result{
                        DispatchQueue.main.async {
                            clouser(true,  .authorized)
                        }
                    }else{
                        DispatchQueue.main.async {
                            clouser(false,  .other)
                        }
                    }
                }
            case .denied:
                clouser(false,  .denied)
            case .authorized:
                clouser(true,  .authorized)
            case .provisional:
                clouser(true,  .authorized)
            case .ephemeral:
                clouser(true,  .authorized)
            @unknown default:
                clouser(false,  .other)
            }
        }
    }
    

}
